package com.example.superoa.model.service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONArray;
import com.example.superoa.model.dao.SalaryAccountDao;
import com.example.superoa.model.dao.SalaryItemDao;
import com.example.superoa.model.dao.SalarySubDao;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.mappers.AccountMapper;
import com.example.superoa.model.pojos.SalaryAccout;
import com.example.superoa.model.pojos.SalaryItem;
import com.example.superoa.model.pojos.SalarySub;
import com.example.superoa.model.pojos.Staff;

@Service
@Transactional(rollbackFor=Exception.class)
public class AccountService {
	@Autowired
	private AccountMapper am;
	@Autowired
	private SalaryAccountDao sad;	
	@Autowired
	private StaffDao sd;      	
	@Autowired
	private SalarySubDao subdao;
	@Autowired
	private SalaryItemDao sid;
	//查询薪资套账所有字段
	public JSONArray findAll(){
		return am.findAllAccount();
	} 
	//通过套账名称查询
	public JSONArray findName(int account_id) {
		return am.findNames(account_id);
	}	
	
	//通过id对数据进行查询（数据初始化）
	public JSONArray bianji(int account_id) {
		return am.findid(account_id);
	}
	//查询套账id=1的员工
	public JSONArray selectId1(int id) {
		return am.findstaff(id);
	}
	//套账查看中的表格
	public JSONArray finditems(int account_id) {
		return am.finditems(account_id);
	}
	//新增套账
	public List<Staff> xinzeng(SalaryAccout sa,int cjr_id,List<Integer> staff_id,String accountName,BigDecimal saBase,String saRemarks) {
		//新增套账
		SalaryAccout salary= new SalaryAccout();
		salary.setAccountName(accountName);
		salary.setSaDass("启用");
		salary.setSaBase(saBase);
		salary.setSaDate(new Date());
		//创建人
		Staff st=sd.findById(cjr_id).get();
		salary.setStaff(st);		
		//套账员工
		List<Staff> sts=new ArrayList();
		if(staff_id.size()!=0) {
			for (Integer  idd : staff_id) {
				Staff staff=sd.findById(idd).get();
				sts.add(staff);
			}
		}
		salary.setStaffs(sts);
		salary.setSaRemarks(saRemarks);
		sad.save(salary);
		Integer sa_id = salary.getAccountId();
		//新增项目五险一金
		SalaryItem it=new SalaryItem();
		it.setItemsName("五险一金");
		it.setSiRemarks("养老保险、医疗保险、失业保险、工伤保险和生育保险，及住房公积金");
		//计算五险一金 个人   工伤保险和生育保险个人不需要缴纳费用
		//养老保险     基本工资*8%
		BigDecimal yanlao=saBase.multiply(new BigDecimal(0.08));
		//医疗保险     基本工资*2%+3
		BigDecimal yiliao=saBase.multiply(new BigDecimal(0.02)).add(new BigDecimal(3));
		//失业保险     基本工资*0.2%
		BigDecimal shiye=saBase.multiply(new BigDecimal(0.002));
		//住房公积金  基本工资*12%
		BigDecimal zhufang=saBase.multiply(new BigDecimal(0.12));
		//五险一金
		BigDecimal zongji=yanlao.add(yiliao).add(shiye).add(zhufang);
		BigDecimal zj=new BigDecimal(0).subtract(zongji);
		it.setSiMoney(zj);
		sid.save(it);
		Integer itemss=it.getItemsId();
			
		//新增项目个人所得税
		SalaryItem it1=new SalaryItem();
		it1.setItemsName("个人所得税");
		it1.setSiRemarks("根据2019年个人所得税税率计算");
		//个税【0-3000   税率3%  扣除数0】【3000-12000 税率20%  扣除数 1410】 【12000-25000 税率20%  扣除数 1410】5000*3%-0
		BigDecimal shui = saBase.subtract(new BigDecimal(5000));
		BigDecimal result;
		if(shui.compareTo(new BigDecimal(0))==1 && shui.compareTo(new BigDecimal(3000))==-1) {
				result=shui.multiply(new BigDecimal(0.03));
		}else if(shui.compareTo(new BigDecimal(3000))>=0 && shui.compareTo(new BigDecimal(12000))==-1) {
				result=shui.multiply(new BigDecimal(0.1)).subtract(new BigDecimal(210));
		}else if(shui.compareTo(new BigDecimal(12000))>=0 && shui.compareTo(new BigDecimal(25000))==-1) {
				result=shui.multiply(new BigDecimal(0.2)).subtract(new BigDecimal(1410));
		}else if(shui.compareTo(new BigDecimal(25000))>=0 && shui.compareTo(new BigDecimal(35000))==-1) {
				result=shui.multiply(new BigDecimal(0.25)).subtract(new BigDecimal(2660));
		}else if(shui.compareTo(new BigDecimal(35000))>=1 && shui.compareTo(new BigDecimal(55000))==-1) {
				result=shui.multiply(new BigDecimal(0.3)).subtract(new BigDecimal(4410));
		}else if(shui.compareTo(new BigDecimal(550000))>=0 && shui.compareTo(new BigDecimal(80000))==-1) {
				result=shui.multiply(new BigDecimal(0.35)).subtract(new BigDecimal(7160));
		}else if(shui.compareTo(new BigDecimal(80000))>=0) {
				result=shui.multiply(new BigDecimal(0.45)).subtract(new BigDecimal(15160));
		}else {
			result=new BigDecimal(0);
		}
		it1.setSiMoney(new BigDecimal(0).subtract(result));
		sid.save(it1);
		Integer itemsid = it1.getItemsId();
		//新增项目薪资中间表
		am.insertxx(sa_id,itemss);
		am.insertxx(sa_id,itemsid);
		return sts;
	}
	//明细表格
	public JSONArray paytable(int account_id) {
		return am.findpay(account_id);
	}
	//明细通过名字查询
	public JSONArray findpayName(String account_name) {
		return am.findpayName(account_name);
	}
	//套账项目
	public JSONArray findsitems() {
		return am.findsitems();		
	}
	//添加项目
	public void additems(int account_id,int items_id) {		
		SalaryItem addi = sid.findById(items_id).get();
		SalaryAccout adda = sad.findById(account_id).get();
		List<SalaryAccout> sa=addi.getSalaryAccouts();
		if(sa==null) {
			sa=new ArrayList<>();
		}
		sa.add(adda);
		addi.setSalaryAccouts(sa);
	}
	//禁用
	public void jinyong(int account_id) {
		SalaryAccout sa = sad.findById(account_id).get();
		sa.setSaDass("禁用");		
		sad.save(sa);
	}
	public JSONArray sa_jingyong() {
		return am.sa_jingyong();
	}
	public void xzitems(String items_name,BigDecimal items_money,String si_remarks,Integer account_id) {
		SalaryItem it=new SalaryItem();
		it.setItemsName(items_name);
		it.setSiMoney(items_money);
		it.setSiRemarks(si_remarks);
		sid.save(it);
		am.insertxx(account_id,it.getItemsId());
	}
}
